package com.winchain.chainbill.bill.biz.repurchase.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.StringUtil;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRpur;
import com.winchain.chainbill.bill.base.service.CbDctQtService;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.biz.Bill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRpurDctQtCmd implements ICommand<Bill<CbRpur>>, ICallback<Bill<CbRpur>, AcctTxRec> {
	@Autowired
	private CbDctService cbDctService;
	@Autowired
	private CbDctQtService cbDctQtService;

	public boolean execute(CommandContext<Bill<CbRpur>> context) throws Exception {
		CbRpur cbRpur = (CbRpur) ((Bill) context.getSource()).getSource();
		CbDct cbDct = (CbDct) this.cbDctService.selectByKey(cbRpur.getDctId());
		if (!StringUtil.emptyAndNull(cbDct.getQtId())) {
			this.cbDctQtService.res(cbDct.getQtId(), cbDct.getDctAmt());
		}
		return false;
	}

	public boolean callback(CallbackContext<Bill<CbRpur>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}
}
